#pragma once

#include <iostream>
#include <pthread.h>
#include <functional>
#include <string>
#include <vector>
#include <unistd.h>
using namespace std;


namespace Thread
{
    template<class T>
    using func_t = function<void(T)>;

    template<class T>
    class thread
    {
    public:
        void Excute()
        {
            _func(_data);
        }
    public:
        thread(func_t<T> func,T data,string name = "none name")
        :_func(func)
        ,_data(data)
        ,_name(name)
        ,stop(true)
        {}
        ~thread()
        {}

        static void* threadrun(void* args)
        {
            thread<T>* self = (thread<T>*)args;
            self->Excute();
            return nullptr;
            
        }
        string getname()
        {
            return _name;
        }
        bool start()
        {
            int n = pthread_create(&_tid,nullptr,threadrun,this);
            if(n == 0)
            {
                stop = false;
                return true;
            }
            else
            {
                return false;
            }
        }
        void Detech()
        {
            if(stop == false)
            {
                pthread_detach(_tid);
            }
        }
        void join()
        {
            if(stop == false)
            {
                pthread_join(_tid,nullptr);
            }
        }
        void Stop()
        {
            stop = true;
        }
    private:
        pthread_t _tid;
        string _name;
        func_t<T> _func;
        T _data;
        bool stop;
    };
};